#include <iostream>

class Cat
{

public:
    std::string name;
    int age;
};

class Person
{

public:
    int age;
    Cat *cat;

    ~Person()
    { // 析构函数
        if (cat != nullptr)
        {
            delete cat;
            cat = nullptr;
        }
    }

    Person()
    {
    }

    Person(const Person &p)
    {
        // 在这个函数中，进行深拷贝
        age = p.age;
        cat = new Cat(*p.cat);
    }
};

int main(int argc, char const *argv[])
{
    Person *p1 = new Person();
    Cat *cat = new Cat();
    p1->cat = cat;
    std::cout << cat << std::endl;
    std::cout << p1->cat << std::endl;

    Person *p2 = new Person(*p1); // 重写的拷贝构造函数，变成了深拷贝

    std::cout << p2->cat << std::endl; // 深拷贝中，p2 的cat 属性是一个新的对象和 p1 的cat 属性的地址不同

    delete p2;

    std::cout << cat << std::endl;
    std::cout << p1->cat << std::endl;

    delete p1;

    std::cout << cat << std::endl;

    return 0;
}
